package zhongbo.java.air.dictionary.service.ServiceImpl;



import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.BusinessException;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.dictionary.mapper.AirCraftMapper;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.service.IAirCraftService;

import java.util.Date;

@Service
public class IAirCraftServiceImpl extends ServiceImpl<AirCraftMapper, AirCraft> implements IAirCraftService{

    @Autowired
    private AirCraftMapper airCraftMapper;

    @Override
    public PageVo<AirCraft> pageQuery(Integer page, Integer pageSize
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
      QueryWrapper<AirCraft> queryWrapper = new QueryWrapper<AirCraft>();
      Page<AirCraft> airCraftPage = new Page<>(page,pageSize);
        if (oderByField == null || oderByField.isEmpty()){
            queryWrapper.orderByAsc("update_time");
        }else {
            if (orderByAsc){
                queryWrapper.orderByAsc(oderByField);
            }else {
                queryWrapper.orderByDesc(oderByField);
            }
        }

        //指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)){
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }

      Page<AirCraft> airCraftPage1 = airCraftMapper.selectPage(airCraftPage, queryWrapper);
      return new PageVo<>(airCraftPage1);
    }

    @Override
    public ResultVo saveAirCraft(AirCraft airCraft) {
        airCraft.setCreateTime(new Date());
        airCraft.setUpdateTime(new Date());

        if (airCraft.getIcaoCode() == null || airCraft.getIcaoCode().isEmpty()){
            throw new BusinessException("地址码不能为空");
        }else if (airCraft.getIcaoCode().length() > 24){
            throw new BusinessException("地址码长度不能大于24位");
        }

        if (airCraft.getRegistration() == null || airCraft.getRegistration().isEmpty()){
            throw new BusinessException("注册号不能为空");
        }else if (airCraft.getRegistration().length() > 20){
            throw new BusinessException("注册号长度不能大于20位");
        }

        if (airCraft.getAirline() == null || airCraft.getAirline().isEmpty()){
            throw new BusinessException("所属公司不能为空");
        }

        if (airCraft.getAircraftType() == null || airCraft.getAircraftType().isEmpty()){
            throw new BusinessException("机型不能为空");
        }

        if (airCraft.getStatus() == null || airCraft.getStatus().isEmpty()){
            throw new BusinessException("所属状态不能为空");
        }

        LambdaQueryWrapper<AirCraft> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AirCraft::getIcaoCode,airCraft.getIcaoCode());
        if (!airCraftMapper.selectList(queryWrapper).isEmpty()){
            throw new BusinessException("地址码只能是唯一业务编号");
        }

        if (airCraftMapper.insert(airCraft) < 0 ){
            return ResultVo.error("添加失败");
        }



        return ResultVo.ok("添加成功!");
    }

    @Override
    public ResultVo updateAirCraft(AirCraft airCraft) {
        airCraft.setUpdateTime(new Date());
        if (airCraft.getId() == null || airCraft.getId().isEmpty()){
            throw new BusinessException("id不能为null");
        }
        LambdaQueryWrapper<AirCraft> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AirCraft::getIcaoCode,airCraft.getIcaoCode());
        if (!airCraftMapper.selectList(queryWrapper).isEmpty()){
            throw new BusinessException("地址码只能是唯一业务编号");
        }


        if (!(airCraftMapper.updateById(airCraft) > 0)){
            return ResultVo.error("修改失败,不存在该航空");
        }
        return ResultVo.ok("修改成功!");
    }


}
